﻿#include <iostream>
#include <thread>
#include <mutex>
std::once_flag initFlag;
void initialize() {
    std::cout << ">>> Initialization function called by thread: "
              << std::this_thread::get_id() << std::endl;
}

void worker(int id) {
    std::cout << "Thread " << id << " started\n";
    // 所有线程都会尝试初始化，但只有一次生效
    std::call_once(initFlag, initialize);
    std::cout << "Thread " << id << " finished\n";
}

int main() {
    std::thread t1(worker, 1);
    std::thread t2(worker, 2);
    std::thread t3(worker, 3);

    t1.join();
    t2.join();
    t3.join();

    return 0;
}
